#include <stdexcept>
#include "NetContextPool.hpp"

namespace TWT {
namespace NETWORK {
NetContextPool::NetContextPool(std::size_t pool_size):
    work_(net::require(ioContext_.get_executor(), net::execution::outstanding_work.tracked))
{
  if (pool_size == 0)
  {
      pool_size = 2;
  }

    for(auto i = pool_size - 1; i > 0; --i)
    {
        threads_.emplace_back([this]
                {
                    ioContext_.run();
                });
    }

}

NetContextPool::~NetContextPool()
{
    for(auto& thread : threads_)
    {
        thread.join();
    }
};


void NetContextPool::stop()
{
    ioContext_.stop();
}

net::io_context& NetContextPool::getIoContext() 
{
    return ioContext_;
}


} // NETWORK
} // TWT
